import 'dart:convert' show json;

import 'package:loxen/mode/material/material_item.dart';
import 'package:loxen/mode/user.dart';
import 'package:loxen/utils/html_util.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class InspirationInfo {

  InspirationInfo(
      {this.id,
      this.contentList,
      this.title,
      this.add_time,
      this.cover,
      this.content,
      this.comment_num,
      this.like_num,
      this.user,
      this.is_collect,
      this.is_like,
      this.view_num,
      this.category,
      this.brief,
      this.collect_num,
      this.images,
      this.share_image,
      this.tag,
      this.products});

  factory InspirationInfo.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }
    final List<String> images = jsonRes['images'] is List ? <String>[] : null;
    if (images != null) {
      for (final dynamic item in jsonRes['images']) {
        if (item != null) {
          images.add(asT<String>(item));
        }
      }
    }
    final List<MaterialItem> products = jsonRes['products'] is List ? <MaterialItem>[] : null;
    if (products != null) {
      for (final dynamic item in jsonRes['products']) {
        if (item != null) {
          products.add(MaterialItem.fromJson(asT<Map<String, dynamic>>(item)));
        }
      }
    }
    var id = jsonRes['id'];
    if (id is String) {
      id = int.parse(id);
    } else {
      id = id;
    }
    return InspirationInfo(
        id: id,
        title: asT<String>(jsonRes['title']),
        add_time: asT<String>(jsonRes['add_time']),
        cover: asT<String>(jsonRes['cover']),
        content: asT<String>(jsonRes['content']),
        comment_num: asT<int>(jsonRes['comment_num']),
        like_num: asT<int>(jsonRes['like_num']),
        user: User.fromJson(asT<Map<String, dynamic>>(jsonRes['user'])),
        is_collect: asT<int>(jsonRes['is_collect']),
        is_like: asT<int>(jsonRes['is_like']),
        view_num: asT<int>(jsonRes['view_num']),
        category: asT<int>(jsonRes['category']),
        brief: asT<String>(jsonRes['brief']),
        collect_num: asT<int>(jsonRes['collect_num']),
        images: images,
        share_image: asT<String>(jsonRes['share_image']),
        tag: asT<String>(jsonRes['tag']),
        products: products);
  }

  int id;
  String title;
  String add_time;
  String cover;
  String content;
  int comment_num;
  int like_num;
  User user;
  int is_collect;
  int is_like;
  int view_num;
  int category;
  String brief;
  int collect_num;
  List<String> images;
  String share_image;
  String tag;
  List<dynamic> contentList;
  List<MaterialItem> products;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'title': title,
        'add_time': add_time,
        'cover': cover,
        'content': content,
        'comment_num': comment_num,
        'like_num': like_num,
        'user': user,
        'is_collect': is_collect,
        'is_like': is_like,
        'view_num': view_num,
        'category': category,
        'brief': brief,
        'collect_num': collect_num,
        'images': images.toString(),
        'share_image': share_image,
        'tag': tag,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
